<?php

class PointsModel extends Model
{
    
    // 退款
    public function refund($sale_id)
    {
        $sale = M('Sale')->where(' id = ' . $sale_id)->find();
        $account_db = M('Member_account_log');
        $list = $account_db->where('sale_id = ' . $sale_id . ' and status = 1 and  pdstate in (3,4)')->select();
        $member_db = M('Member');
        
        foreach ($list as $k => $v) {
            // if($v['']){}
            $data = array();
            $data = $v;
            unset($data['id']); 
            $data['pdstate'] = 0;
            $data['attr'] = 0;
            $data['remark'] = "订单:\n" . $v['order_sn'] . '退款';
            $data['dateline'] = time();
            $data['changenum'] = ($data['changenum'] * - 1);
			$data['total'] = $data['total']+$data['changenum'];
            if ($account_db->add($data)) {
                $member = $member_db->where(' id = ' . $v['member_id'])->find();
                
                $member_db->where('id= ' . $v['member_id'])->save(array('money'=>($member['money']+$data['changenum'])));
            }
        }
    }

    public function deduction($sale_id)
    {
        $points_log = M('Points_log');
        
        $data = $points_log->where('sale_id = ' . $sale_id)->select();
        
        $sale = M('sale')->where(' id = ' . $sale_id)->find();
        $member = M('Member')->where(' id = ' . $sale_id['member_id'])->getField('nickname');
        
        if (! $this->test($sale_id)) {
            return false;
        }
        if (! $data) {
            return false;
        }
        // exit(dump($data));
        
        $points_db = M('Points');
        
        foreach ($data as $k => $v) {
            
            $points = $points_db->where('member_id = ' . $v['member_id'])->find();
            $updata['total'] = ($points['total'] - $v['total']);
            $points_db->where('member_id = ' . $v['member_id'])->save($updata);
            $data = $v;
            unset($data['id']);
            $data['remark'] = $member . '用户退款，扣除积分';
            $data['total'] = ($v['total']) * - 1;
            $points_log->add($data);
        }
        
        /*
         * $member_account_db = M('Member_account_log');
         * $account_log = $member_account_db->where('sale_id = ' . $sale_id)->find();
         *
         * if ($account_log) {
         * $ret=M('Member')->where('id = ' . $account_log['member_id'])->setDec('money', $account_log['changenum']);
         * dump($account_log['changenum']);
         * if ($ret) {
         * $data = $account_log;
         * $data['changenum'] = ($data['changenum']) * - 1;
         * $data['dateline']=time();
         * $data['type']=0;
         * $data['attr']=0;
         * $data['remark']='用户退款:'.$data['order_sn'];
         * exit(dump($data));
         * $member_account_db->add($data);
         * }
         * }
         */
    }

    public function test($sale_id)
    {
        $sale = M('Sale')->where(' id = ' . $sale_id)->find();
        $list = M('Sale')->where('order_id = ' . $sale['order_id'] . ' and supplier_id = ' . $sale['supplier_id'])->select();
        
        if ($sale['status'] == 0) {
            $fee = 1;
            $price = 0;
            foreach ($list as $k => $v) {
                if ($v['pay_status'] != 3) {
                    $fee = 0;
                }
                $price += ($v['price'] * $v['buy_num']);
            }
            if ($fee == 1) {
                $fee = M('Order_supplier')->where(' order_id = ' . $sale['order_id'] . ' and price = ' . $price)->getField('shipping_fee');
            }
        } else {
            $fee = 0;
			
        }
        $order = M('Order')->where(' id = ' . $sale['order_id'])->find();
        $member = M('Member')->where(' id = ' . $sale['member_id'])->find();
        $product = M('Product')->where(' id = ' . $sale['product_id'])->getField();
        $data['offline'] = ($sale['price'] * $sale['buy_num']) * 1.06 + $fee;
        $data['remark'] = '订单' . $order['ordernumber'] . ',商品ID' . $sale['product_id'] . '退货。';
        $data['member_id'] = $sale['member_id'];
        $data['time'] = time();
        $data['online'] = 0;
        $data['total'] = $member['offline'] + $data['offline'];
        $data['order_id'] = $sale['order_id'];
        $data['sale_id'] = $sale['id'];
        
        $res = M('Member')->where(' id = ' . $member['id'])->save(array(
            'offline' => ($member['offline'] + $data['offline'])
        ));
        
        if ($res) {
            
            return M('Integral_log')->data($data)->add();
        } else {
            return false;
        }
    }
}